<h2>Search results</h2>
<?php
	$query = $_POST['search'];
	
	# images path
	$IMAGE_PATH = 'images/store/';
	
	# URL variable
	$arg = "search";
	
	# include pic and info context if ID is set
	if(isset($query)) {	
		$conn = connectDB();
		
		// query DB
		$start_timer = microtime_float();
		$result = queryDB("SELECT id, filename, category, location_name, country FROM image WHERE filename LIKE '%$query%' OR location_name LIKE '%$query%' OR country LIKE '%$query%'", $conn);
		$result2 = queryDB("SELECT picid FROM wikiarticle WHERE title LIKE '%$query%' OR summary LIKE '%$query%' OR wikiurl LIKE '%$query%'", $conn);
		$result3 = queryDB("SELECT picid FROM related WHERE title LIKE '%$query%' OR summary LIKE '%$query%'", $conn);
		$end_timer = microtime_float();
		
		$search_time = $end_timer - $start_timer;

		printf("Search done in %1.4f seconds for '%s', ", $search_time, $query);

		# collect results from both search tables
		$total_results = array();
		$t_res = $result;
		
		while($row = mysql_fetch_array($result, MYSQL_NUM)) {
			array_push($total_results, $row[0]);
			array_push($total_results, $row[1]);
		}
		
		$t_res2 = $result2;

		while($row = mysql_fetch_array($result2, MYSQL_NUM)) {
			if(!in_array($row[0], $total_results, TRUE)) {
				array_push($total_results, $row[0]);
				# get filename
				$tmp_res = queryDB("SELECT filename FROM image WHERE id = '$row[0]'", $conn);
				$tmp = mysql_fetch_array($tmp_res, MYSQL_NUM);
				array_push($total_results, $tmp[0]);
			}
		}
		
		$t_res3 = $result3;
		while($row = mysql_fetch_array($result3, MYSQL_NUM)) {
			if(!in_array($row[0], $total_results, TRUE)) {
				array_push($total_results, $row[0]);
				# get filename
				$tmp_res = queryDB("SELECT filename FROM image WHERE id = '$row[0]'", $conn);
				$tmp = mysql_fetch_array($tmp_res, MYSQL_NUM);
				array_push($total_results, $tmp[0]);
			}
		}				
		
		$num = count($total_results);
		
		echo 'gave <strong>'.($num/2).'</strong> corresponding results.<br><br>';
		
		// get row of results from query
		if($num > 0) {
			echo "<table border='0' cellspacing='10' cellpadding='2'>";
		}
		$k = 0;
		while ($k < count($total_results)) {
			echo    "<tr>
								<td width='250' rowspan='4' bgcolor='#333333' align='center'><a href=\"index.php?show=pic&id=".$total_results[$k]."\" target=\"_self\"><img src='thumbnailer.php?src=".$total_results[$k+1]."&x=250&y=187&f=0'/></a>												</td>
								<td>ID: ".$total_results[$k]."</td>
							</tr>
							<tr>
								<td>Filename: ".$total_results[$k+1]."</td>
							</tr>
							<tr>
								<td></td>
							</tr>
							<tr>
								<td></td>
							</tr>
							<tr>
								<td colspan='2'><hr></td>
							</tr>";
			
			$k = $k + 2;
		}
		if($num > 0) {	
			echo "</table>";
		}
	}	
	closeDB($conn);
?>

